package com.goldmantis.alitrip.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goldmantis.alitrip.common.BaseService;
import com.goldmantis.alitrip.common.SysLogUtil;
import com.goldmantis.alitrip.common.sysenum.EnumLogCatagory;
import com.goldmantis.alitrip.common.sysenum.EnumOperationType;
import com.goldmantis.alitrip.mapper.SysDataPrivilegesMapper;
import com.goldmantis.alitrip.pojo.SysDataPrivileges;
import com.goldmantis.alitrip.pojo.SysLog;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Transformer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author:Shmily
 * @Description:
 * @Date:Create in 10:46 2018/2/28
 */
@Service
public class SysDataPrivilegesService extends BaseService<SysDataPrivileges> {

    @Autowired
    private SysDataPrivilegesMapper sysDataPrivilegesMapper;

    @Autowired
    private SysLogService sysLogService;

    public void insertBatch(SysDataPrivileges sysDataPrivileges) {

        SysDataPrivileges itemTemp = new SysDataPrivileges();
        itemTemp.setModuleid(sysDataPrivileges.getModuleid());
        itemTemp.setUserid(sysDataPrivileges.getUserid());

        //记录原先数据
        List<SysDataPrivileges> dataPrivilegesOrginal = this.sysDataPrivilegesMapper.select(itemTemp);

        List<Long> newItems = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(sysDataPrivileges.getDeptIds())) {

            for (Long deptId : sysDataPrivileges.getDeptIds()) {

                //防止重复添加
                boolean flag = false;
                for (SysDataPrivileges itemOriginal : dataPrivilegesOrginal) {
                    if (itemOriginal.getDepid().longValue() == deptId.longValue()
                            && itemOriginal.getUserid().longValue() == sysDataPrivileges.getUserid().longValue()
                            && itemOriginal.getModuleid().longValue() == sysDataPrivileges.getModuleid().longValue()) {
                        flag = true;
                        break;
                    }
                }

                if (!flag) {
                    SysDataPrivileges item = new SysDataPrivileges();
                    item.setDepid(deptId);
                    item.setUserid(sysDataPrivileges.getUserid());
                    item.setModuleid(sysDataPrivileges.getModuleid());
                    super.insert(item);

                    SysLog baseSysLog = SysLogUtil.getBaseSysLog();
                    baseSysLog.setCategoryId(EnumLogCatagory.operation.getValue());
                    baseSysLog.setDescription("用户分配数据权限");
                    baseSysLog.setOperateTypeId(String.valueOf(EnumOperationType.grant.getValue()));
                    baseSysLog.setOperateType(EnumOperationType.grant.getText());
                    baseSysLog.setSourceObjectId("部门Id"+String.valueOf(deptId)+"人员Id"+String.valueOf(sysDataPrivileges.getUserid()));
                    baseSysLog.setSourceContentJson(item.toString());


                    sysLogService.insert(baseSysLog);
                }
                newItems.add(deptId);

            }
        }

        //删除掉以前有、现在没有的数据
        for (SysDataPrivileges itemOriginal : dataPrivilegesOrginal) {
            boolean flag = false;
            for (Long itemNew : newItems) {
                if (itemOriginal.getDepid().longValue() == itemNew.longValue()) {
                    flag = true;
                    break;
                }
            }

            if (!flag) {
                this.sysDataPrivilegesMapper.delete(itemOriginal);

                SysLog baseSysLog = SysLogUtil.getBaseSysLog();
                baseSysLog.setCategoryId(EnumLogCatagory.operation.getValue());
                baseSysLog.setDescription("用户撤销数据权限");
                baseSysLog.setOperateTypeId(String.valueOf(EnumOperationType.revoke.getValue()));
                baseSysLog.setOperateType(EnumOperationType.revoke.getText());
                baseSysLog.setSourceObjectId("部门Id"+itemOriginal.getDepid()+"人员Id"+String.valueOf(itemOriginal.getUserid()));
                baseSysLog.setSourceContentJson(itemOriginal.toString());


                sysLogService.insert(baseSysLog);
            }
        }
    }

    public List<Long> selectBySystemIdAndUserId(Long systemId, Long userId) {

        SysDataPrivileges item = new SysDataPrivileges();
        item.setUserid(userId);
        item.setModuleid(systemId);

        List<SysDataPrivileges> sysDataPrivileges = this.sysDataPrivilegesMapper.select(item);

        List<Long> list = new ArrayList<>();

        CollectionUtils.collect(sysDataPrivileges, new Transformer<SysDataPrivileges, Long>() {
            @Override
            public Long transform(SysDataPrivileges input) {
                return input.getDepid();
            }
        }, list);

        return list;
    }

    public PageInfo<Map> getUserDataPrivilegesByUserId(Long userId, Long systemId, String orgName, Integer offset, Integer limit,boolean subdivision) {
        PageHelper.startPage(offset, limit);
        List<Map> list = this.sysDataPrivilegesMapper.getUserDataPrivilegesByUserId(userId, systemId, orgName,subdivision);
        return new PageInfo<Map>(list);
    }
}
